package com.designMode.singleton;

/**
 * 全局只有一个实例
 * 一个静态类，其余全是实例化
 *
 */
public class Singleton1 {
    private volatile static Singleton1 singleton1 = null;

    private String name = "luolin";
    private Singleton1(){
    }

    public static  Singleton1 createSingleton(){

        if (singleton1 == null){
            synchronized (Singleton1.class) {
                if (singleton1 == null)
                singleton1 = new Singleton1();
            }
        }
        return singleton1;
    }

    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        Runnable r = () ->{
            for (int i = 0;i< 100;i++){

            }
            Singleton1 singleton1 = Singleton1.createSingleton();
            System.out.println(singleton1);
        };

        Runnable runnable = () ->{
            Singleton1 singleton11 = Singleton1.createSingleton();
            System.out.println(singleton11);
        };

        Runnable runnable1 = () ->{
            Singleton1 singleton11 = Singleton1.createSingleton();
            System.out.println(singleton11.getName());
        };
        new Thread(r).start();
        new Thread(runnable).start();
        new Thread(runnable1).start();
    }

}
